package com.libraries.image.activity;

import java.io.File;
import java.util.ArrayList;

import com.example.libraries.R;
import com.libraries.base.BaseActivity;
import com.libraries.helper.FileHelper;
import com.libraries.helper.ImgHelper;
import com.libraries.helper.Logger;
import com.libraries.image.adapter.ImageGalleryAdapter;
import com.libraries.image.adapter.ImagePagerAdapter;
import com.libraries.image.view.PreviewImgView;
import com.libraries.view.HorizontalListView;

import android.annotation.SuppressLint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

@SuppressLint("NewApi")
public class ImageGalleryActivity extends BaseActivity implements OnClickListener,OnPageChangeListener,OnItemClickListener{
	private int statusHeight;// 状态栏的高度
	private int windowW, windowH;
	private ArrayList<String> lImages;
	private ArrayList<PreviewImgView> lImageViews;
	
	private ImagePagerAdapter pagerAdapter;
	private ImageGalleryAdapter galleryAdapter;
	
	private TextView tv_name;
	private TextView tv_progress;
	private ViewPager vp_gallery; 
	private HorizontalListView hlv_gallery;
	private View v_gallery;
	private int currIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_gallery);
		createTitleBar();
		initGalleryData();
		initGalleryView();
	}

	@SuppressWarnings("deprecation")
	private void initGalleryData() {
		lImages=new ArrayList<String>();
		FileHelper.loadLocalImages(this,refreshHandler);
		lImageViews=new ArrayList<PreviewImgView>();
		windowW = getWindowManager().getDefaultDisplay().getWidth();
		windowH = getWindowManager().getDefaultDisplay().getHeight();
	}
	
	private void createTitleBar(){
		tv_name=(TextView) findViewById(R.id.tv_name);
		findViewById(R.id.ibtn_back).setOnClickListener(this);
		findViewById(R.id.ibtn_share).setOnClickListener(this);
		findViewById(R.id.ibtn_more).setOnClickListener(this);
		findViewById(R.id.ibtn_delete).setOnClickListener(this);
	}

	private void initGalleryView() {
		v_gallery=findViewById(R.id.rl_gallery_listview);
		vp_gallery=(ViewPager) findViewById(R.id.vp_gallery);
		tv_progress=(TextView) findViewById(R.id.tv_progress);
		pagerAdapter=new ImagePagerAdapter(lImageViews);
		vp_gallery.setOnPageChangeListener(this);
		vp_gallery.setAdapter(pagerAdapter);
		
		hlv_gallery=(HorizontalListView) findViewById(R.id.hlv_gallery);
		galleryAdapter=new ImageGalleryAdapter(this, lImages);
		hlv_gallery.setOnItemClickListener(this);
		hlv_gallery.setAdapter(galleryAdapter);
	}
	
	private void reloadImageViews(){
		Logger.log(this, "reloadImageViews",lImages.size());
		if (lImages.size()==0) {
			return;
		}
		lImageViews.clear();
		for (int vari = 0; vari < lImages.size(); vari++) {
			lImageViews.add(vari,getPreviewView());
			if (vari==0) {
				ImgHelper.loadFileByFresco(lImageViews.get(0), lImages.get(0), null);
			}
			else {
				ImgHelper.loadResByFresco(lImageViews.get(vari), R.drawable.ic_default_banner, null);
			}
		}
		setCurrItemInfo(0);
		galleryAdapter.notifyDataSetChanged(lImages);
		pagerAdapter.notifyDataSetChanged(lImageViews);
	}
	
	private PreviewImgView getPreviewView(){
		PreviewImgView previewView = new PreviewImgView(this);
		previewView.setBackgroundResource(R.color.transparent);
		previewView.setLayoutParams(new LayoutParams(1,1));
		previewView.setScaleType(ScaleType.CENTER_CROP);
		previewView.setActivity(this);// 注入Activity.
		/** 测量状态栏高度 **/
		previewView.getViewTreeObserver().addOnGlobalLayoutListener(new IGlobalLayoutListener(previewView));
		return previewView;
	}
	

	@Override
	public void onPageScrollStateChanged(int index) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int index) {
		Logger.log(this, "onPageSelected", index);
		setCurrItemInfo(index);
	}
	
	private void setCurrItemInfo(int index){
		currIndex=index;
		File file=new File(lImages.get(index));
		tv_name.setText(file.getName());
		galleryAdapter.setCurrIndex(index);
		hlv_gallery.setSelection(index);
		tv_progress.setText((index+1)+"/"+lImages.size());
		PreviewImgView previewView=lImageViews.get(index);
		ImgHelper.loadFileByFresco(previewView,file.getAbsolutePath(), null);
	}

	private Handler refreshHandler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Logger.log(this, "handleMessage",galleryAdapter, lImages.size());
			lImages=(ArrayList<String>) msg.obj;
			if (galleryAdapter != null) {
				reloadImageViews();
			}
		}
	};

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		vp_gallery.setCurrentItem(position,true); 
	}
	
	private class IGlobalLayoutListener implements OnGlobalLayoutListener{
		private PreviewImgView previewView;
		public IGlobalLayoutListener(PreviewImgView imgView) {
			this.previewView=imgView;
		}
		@Override
		public void onGlobalLayout() {
			Logger.log(this, "onGlobalLayout", statusHeight);
			if (statusHeight == 0) {
				// 获取状况栏高度
				Rect frame = new Rect();
				getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
				statusHeight = frame.top;
				previewView.setScreen_H(windowW - statusHeight);
				previewView.setScreen_W(windowH);
			}
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.ibtn_delete) {
			int index=vp_gallery.getCurrentItem();
			String imgPath=lImages.get(index);
			new File(imgPath).delete();
			lImageViews.remove(index);
			lImages.remove(index);
			if (index==0) {
				setCurrItemInfo(index+1);
			}
		} else if (id == R.id.ibtn_share) {
			
		} else if (id == R.id.ibtn_more) {
		} else {
			finish();
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode==KeyEvent.KEYCODE_BACK) {
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
